﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
using Estimate.Common.Estimate;
using Estimate.Common.DBFieldTypes;
using Estimate.Common.Entity;
using Estimate.Common.DataFileFinder;
using Estimate.Reports;

namespace EstimateService
{
    public partial class CollectionRankReportHandler : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string begindate = Request.QueryString["begindate"] != null ? Request.QueryString["begindate"].ToString() : "";
            string enddate = Request.QueryString["enddate"] != null ? Request.QueryString["enddate"].ToString() : "";
            string tabletype = Request.QueryString["tabletype"] != null ? Request.QueryString["tabletype"].ToString() : "";
            string semitype = Request.QueryString["semitype"] != null ? Request.QueryString["semitype"].ToString() : "";
            string spans = Request.QueryString["spans"] != null ? Request.QueryString["spans"].ToString() : "";
            string tid = Request.QueryString["tid"] != null ? Request.QueryString["tid"].ToString() : "";

            if (begindate != "" && enddate != "" && tabletype != "" && semitype != "" && spans != "" && tid != "")
                Response.Write(CollectionRankReport(begindate, enddate, tabletype, semitype, spans, tid));
        }

        /// <summary>
        /// 集体排名表
        /// </summary>
        /// <param name="begindate">开始日期</param>
        /// <param name="enddate">结束日期</param>
        /// <param name="tabletype"></param>
        /// <param name="semitype"></param>
        /// <param name="spans"></param>
        /// <param name="tid">执行任务号</param>
        /// <returns></returns>
        public string CollectionRankReport(string begindate, string enddate, string tabletype, string semitype, string spans, string tid)
        {
            string connectionString = ConfigurationManager.AppSettings["ConnectionString"];

            DateTime beginDate = DateTime.Parse(begindate);
            DateTime endDate = DateTime.Parse(enddate);
            CollectionRankType tableType = (CollectionRankType)Enum.Parse(typeof(CollectionRankType), tabletype);
            ForeSemiType semiType = (ForeSemiType)Enum.Parse(typeof(ForeSemiType), semitype);

            List<ForeSpanType> spanList = new List<ForeSpanType>();
            foreach (string s in spans.Split(','))
                spanList.Add((ForeSpanType)Enum.Parse(typeof(ForeSpanType), s.Replace("'", "")));
            ForeSpanType[] spanArray = spanList.ToArray();

            ForeSendpointType[] sendPoints = new ForeSendpointType[] { ForeSendpointType.H06, ForeSendpointType.H16 };

            Estimate.Reports.CollectionRankReport collectionRankReport = new Estimate.Reports.CollectionRankReport(connectionString, tableType, tid);
            collectionRankReport.PrepareData(beginDate, endDate,semiType, spanArray, sendPoints );
            return collectionRankReport.GetCityScoreResultString();

        }

    }
}